package io.github.talelin.latticy.controller.v1.ag;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.ag.monitoringroom.CreateOrUpdateMonitoringRoomDTO;
import io.github.talelin.latticy.model.ag.MonitoringRoomDO;
import io.github.talelin.latticy.service.ag.MonitoringRoomService;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;
import java.util.List;

@Validated
@RestController
@RequestMapping("/v1/ag/monitoringroom")
public class MonitoringRoomController {

    @Autowired
    private MonitoringRoomService monitorRoomService;

    @GetMapping("/{id}")
    public MonitoringRoomDO getMonitorRoom(@PathVariable("id") @Positive(message = "id必须为正整数") int id){
        MonitoringRoomDO room = monitorRoomService.getById(id);
        if (room==null){
            throw new NotFoundException("未找到被监控的房间",10023);
        }
        return room;
    }
    @GetMapping("/list")
    public List<MonitoringRoomDO> getMonitorRooms(){
        List<MonitoringRoomDO> rooms = monitorRoomService.getMonitoringRooms();
        if (rooms==null){
            throw new NotFoundException("未找到被监控的房间",10023);
        }
        return rooms;
    }
    @GetMapping("/page")
    public PageResponseVO<MonitoringRoomDO> page(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{count}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page}") Integer page
    ) {
        Page<MonitoringRoomDO> pager = new Page<>(page, count);
        IPage<MonitoringRoomDO> paging =monitorRoomService.getMonitoringByPage(pager);
        return PageUtil.build(paging);
    }

    @GetMapping("/search")
    public PageResponseVO<MonitoringRoomDO> search(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{count}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page}") Integer page,
            @RequestParam(name = "keyword") String keyword
    ) {
        Page<MonitoringRoomDO> pager = new Page<>(page, count);
        IPage<MonitoringRoomDO> paging = monitorRoomService.searchMonitoringByKeyword(pager, keyword);
        return PageUtil.build(paging);
    }

    @PostMapping("/create")
    public CreatedVO createMonitorRoom(@RequestBody @Validated CreateOrUpdateMonitoringRoomDTO validator){
        monitorRoomService.createRoom(validator);
        return new CreatedVO("创建监控对象房间成功");
    }

    @DeleteMapping("/{id}")
    @PermissionMeta(value = "删除监控房间", module = "监控房间", mount = true)
    @GroupRequired
    public DeletedVO deletedMonitorRoom(@PathVariable("id") @Positive(message = "{id}") int id){
        MonitoringRoomDO md = monitorRoomService.getById(id);
        if (md==null){
            throw new NotFoundException("MonitoringRoom not found",10022);
        }
        monitorRoomService.deleteById(md.getId());
        return new DeletedVO(16);
    }

    @PutMapping("/{id}")
    public UpdatedVO updatedMonitorRoom(@PathVariable("id") @Positive(message = "{id.positive}") Integer id,
                                        @RequestBody @Validated CreateOrUpdateMonitoringRoomDTO validator){
        MonitoringRoomDO monitoringRoom = monitorRoomService.getById(id);
        if (monitoringRoom == null){
            throw new NotFoundException("MonitoringRoom not found",10022);
        }
        monitorRoomService.updateMonitoringRoom(monitoringRoom,validator);
        return new UpdatedVO("更新监控房间成功");
    }
}
